from reportlab.lib.pagesizes import letter
from reportlab.lib import colors
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle

def generate_pdf_with_table(file_path, data):
    # 创建 PDF 文档
    doc = SimpleDocTemplate(file_path, pagesize=letter)
    elements = []

    # 定义表格数据
    table_data = [
        ['Name', 'Age', 'Gender'],
        ['John', '30', 'Male'],
        ['Alice', '25', 'Female'],
        ['Bob', '35', 'Male']
    ]

    # 创建表格对象
    table = Table(table_data)

    # 定义表格样式
    style = TableStyle([
        ('BACKGROUND', (0, 0), (-1, 0), colors.gray),
        ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
        ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
        ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
        ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
        ('BACKGROUND', (0, 1), (-1, -1), colors.beige),
    ])
    table.setStyle(style)

    # 将表格对象添加到文档中
    elements.append(table)
    doc.build(elements)

if __name__ == "__main__":
    # 生成带有表格的 PDF 文档
    generate_pdf_with_table("table_example.pdf", [])

